package com.kanshu.ksgb.fastread.doudou.module.makemoney.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.base.baseadapter.CustomFragmentAdapter;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.interfaces.ISwitchTab;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.view.TitlebarView;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.OnTabSelectListener;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wcy on 18/5/13
 */
@Route(path = ARouterConfig.HOME_PAGE_MAKE_MONEY)
public class MakeMoneyFragment extends BaseFragment implements ISwitchTab {
    public static final int SUB_TAB_APPRENTICE = 0;
    public static final int SUB_TAB_EVERYDAY = 1;
    public static final int SUB_TAB_WEEKLY = 2;
    public static final String EXTRA_SUB_TAB = "sub_tab_index";
    @BindView(R.id.pager)
    ViewPager mPager;
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    Unbinder unbinder;
    @BindView(R.id.title_place_holder)
    View mTitlePlaceHolder;
    @BindView(R.id.title_layout)
    TitlebarView mTitle;
    private int mSubTabIndex;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_make_money_layout, null);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        parseArguments();
        int statusBarHeight = DisplayUtils.getStatusBarHeight(getActivity());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTitlePlaceHolder.getLayoutParams();
        params.height = statusBarHeight;
        mTitlePlaceHolder.setLayoutParams(params);
        mTitle.goneLeftContainer();
        mTitle.goneRightContainer();
        mTitle.setTitle("赚钱");
        List<Fragment> list = new ArrayList<>();
        list.add(new ApprenticeFragment());
        list.add(new EverydayFragment());
        list.add(new WeeklyFragment());
        String[] strs = new String[]{"收徒赚钱", "每日赚钱", "每周赚钱"};
        CustomFragmentAdapter pagerAdapter = new CustomFragmentAdapter(getChildFragmentManager(), list);
        mPager.setAdapter(pagerAdapter);
        mTabLayout.setViewPager(mPager, strs);
        mPager.setOffscreenPageLimit(strs.length);
        if (mSubTabIndex >= 0 && mSubTabIndex < list.size()) {
            mTabLayout.setCurrentTab(mSubTabIndex);
        }
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mSubTabIndex = position;
                AdPresenter.pvuvStaticsByString(getResources().getStringArray(R.array.ZQTOP_POSITION)[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    public static MakeMoneyFragment newIntance(int subTabIndex) {
        MakeMoneyFragment fragment = new MakeMoneyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(EXTRA_SUB_TAB, subTabIndex);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void parseArguments() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mSubTabIndex = bundle.getInt(EXTRA_SUB_TAB);
    }
    @Override
    public void switchTab(int tab) {
        if (tab >= 0 && mTabLayout != null && tab < mTabLayout.getTabCount()) {
            mTabLayout.setCurrentTab(tab);
            mSubTabIndex = tab;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
